﻿' Frowser - http://www.frowser.net
' Copyright (c) 2010
' by Will Morgenweck, Active Modules, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.ApplicationBlocks.Data
Imports System.Configuration
Imports Active.Modules.Forums
Namespace Frowser.Controllers
    Public Class ActiveForums
        Inherits Data
        Public Function ForumsList(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal u As UserInfo) As List(Of ForumInfo)
            Dim forumIds As String = GetForumsForUser(u.UserRoles, PortalId, ModuleId, "CanRead")
            Dim ds As DataSet = CType(SqlHelper.ExecuteDataset(connectionString, dbPrefix & "UI_ForumView", PortalId, ModuleId, u.UserId, False, -1, forumIds), DataSet)
            Dim ft As DataTable = ds.Tables(0).DefaultView.ToTable
            Dim fi As ForumInfo = Nothing
            Dim fc As New List(Of ForumInfo)
            For Each dr As DataRow In ft.Rows
                fi = New ForumInfo
                With fi
                    .ModuleId = Integer.Parse(dr("ModuleId").ToString)
                    .ForumId = Integer.Parse(dr("ForumId").ToString)
                    .ForumName = dr("ForumName").ToString
                    .GroupName = dr("GroupName").ToString
                    .ForumDescription = dr("ForumDesc").ToString
                    .GroupId = Integer.Parse(dr("ForumGroupId").ToString)
                    .LastPostSubject = dr("LastPostSubject").ToString
                    .LastPostUserId = Integer.Parse(dr("LastPostAuthorId").ToString)
                    .TotalTopics = Integer.Parse(dr("TotalTopics").ToString)
                    .TotalReplies = Integer.Parse(dr("TotalReplies").ToString)
                    .CanCreate = dr("CanCreate").ToString
                    .CanRead = dr("CanRead").ToString
                    .CanReply = dr("CanReply").ToString
                End With
                fc.Add(fi)

            Next
            Return fc
        End Function
        Public Function GetForums(ByVal PortalId As Integer, ByVal ModuleId As Integer) As ForumCollection
            Dim f As New ForumCollection
            Dim fi As Forum = Nothing
            Dim cKey As String = String.Format(CacheKeys.ForumList, PortalId & "mobile")
            Dim obj As Object = DataCache.CacheRetrieve(cKey)
            obj = Nothing
            If Not obj Is Nothing Then
                f = CType(obj, ForumCollection)
                Return f
            End If

            Using dr As IDataReader = SqlHelper.ExecuteReader(connectionString, dbPrefix & "ForumsList", PortalId, -1)
                While dr.Read
                    fi = New Forum
                    With fi
                        .ModuleId = Integer.Parse(dr("ModuleId").ToString)
                        .ForumID = Integer.Parse(dr("ForumId").ToString)
                        .ForumName = dr("ForumName").ToString
                        .GroupName = dr("GroupName").ToString
                        .ForumDesc = dr("ForumDesc").ToString
                        .ForumGroupId = Integer.Parse(dr("ForumGroupId").ToString)
                        .Active = Boolean.Parse(dr("Active").ToString)
                        With .Security
                            .Announce = dr("CanAnnounce").ToString
                            .Attach = dr("CanAttach").ToString
                            .Create = dr("CanCreate").ToString
                            .Delete = dr("CanDelete").ToString
                            .Edit = dr("CanEdit").ToString
                            .Lock = dr("CanLock").ToString
                            .ModApprove = dr("CanModApprove").ToString
                            .ModDelete = dr("CanModDelete").ToString
                            .ModEdit = dr("CanModEdit").ToString
                            .ModLock = dr("CanModLock").ToString
                            .ModMove = dr("CanModMove").ToString
                            .ModPin = dr("CanModPin").ToString
                            .ModSplit = dr("CanModSplit").ToString
                            .ModUser = dr("CanModUser").ToString
                            .Pin = dr("CanPin").ToString
                            .Poll = dr("CanPoll").ToString
                            .Block = dr("CanBlock").ToString
                            .Read = dr("CanRead").ToString
                            .Reply = dr("CanReply").ToString
                            .Subscribe = dr("CanSubscribe").ToString
                            .Trust = dr("CanTrust").ToString
                            .View = dr("CanView").ToString
                        End With
                    End With
                    f.Add(fi)
                End While
                dr.Close()
            End Using
            DataCache.CacheStore(cKey, f)
            Return f
        End Function
        Public Function GetTopics(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal ForumId As Integer, ByVal u As UserInfo, ByVal RowIndex As Integer, ByVal PageSize As Integer) As List(Of ContentInfo)
            Dim tl As List(Of ContentInfo) = Nothing
            Dim ds As DataSet = SqlHelper.ExecuteDataset(connectionString, dbPrefix & "UI_TopicsView", PortalId, ModuleId, ForumId, u.UserId, RowIndex, PageSize, False, "ReplyCreated")
            Dim dt As DataTable = ds.Tables(3)
            Dim sec As DataRow = ds.Tables(1).Rows(0)
            If Permissions.HasPerm(sec("CanRead").ToString, u.UserRoles) Then
                tl = New List(Of ContentInfo)
                For Each dr As DataRow In dt.Rows
                    Dim ti As ContentInfo = FillContent(dr)
                    tl.Add(ti)
                Next
            End If
            Return tl
        End Function
        Public Function GetTopic(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal ForumId As Integer, ByVal TopicId As Integer, ByVal u As UserInfo, ByVal RowIndex As Integer, ByVal PageSize As Integer) As List(Of ContentInfo)
            Dim tl As List(Of ContentInfo) = Nothing
            Dim tc As ContentInfo = Nothing
            Dim ds As DataSet = SqlHelper.ExecuteDataset(connectionString, dbPrefix & "UI_TopicView", PortalId, ModuleId, ForumId, TopicId, u.UserId, RowIndex, PageSize, False, "ASC")
            If ds.Tables.Count = 0 Then
                Return Nothing
            End If
            Dim drForum As DataRow = ds.Tables(0).Rows(0)
            Dim drSecurity As DataRow = ds.Tables(1).Rows(0)
            Dim dtTopic As DataTable = ds.Tables(2)
            If dtTopic.Rows.Count = 0 Then
                Return Nothing
            End If
            Dim sb As New StringBuilder
            If Permissions.HasPerm(drSecurity("CanRead").ToString, u.UserRoles) Then
                tl = New List(Of ContentInfo)
                For Each dr As DataRow In dtTopic.Rows
                    Dim ti As ContentInfo = FillContent(dr)
                    tl.Add(ti)
                Next
            End If
            Return tl
        End Function
        Private Function FillContent(ByVal dr As DataRow) As ContentInfo
            Dim ti As New ContentInfo
            With ti
                .ForumId = Integer.Parse(dr("ForumId").ToString)
                .TopicId = Integer.Parse(dr("TopicId").ToString)
                .Subject = CleanOutput(dr("Subject").ToString)
                .Body = CleanOutput(dr("Body").ToString)
                .ReplyCount = Integer.Parse(dr("ReplyCount").ToString)
                .DateCreated = dr("DateCreated").ToString
                .AuthorId = Integer.Parse(dr("AuthorId").ToString)
                Try
                    .AuthorName = dr("AuthorDisplayName").ToString
                Catch ex As Exception
                    .AuthorName = dr("DisplayName").ToString
                End Try


            End With
            Return ti
        End Function
        Private Function CleanOutput(ByVal text As String) As String
            If String.IsNullOrEmpty(text) Then
                Return String.Empty
            End If
            text = HttpUtility.HtmlDecode(text)
            text = text.Replace("<br />", ControlChars.NewLine)
            text = text.Replace("<br>", ControlChars.NewLine)
            text = text.Replace("</p>", ControlChars.NewLine)
            text = text.Replace("<p>", String.Empty)
            Dim pattern As String = "<(.|\n)*?>"
            Return Regex.Replace(text, pattern, String.Empty).Trim
        End Function
        Private Function GetForumsForUser(ByVal UserRoles As String, ByVal PortalId As Integer, ByVal ModuleId As Integer, Optional ByVal PermissionType As String = "CanView") As String
            Dim db As New Controllers.ActiveForums
            Dim forumIds As String = String.Empty
            Dim fc As ForumCollection = db.GetForums(PortalId, ModuleId)
            For Each f As Forum In fc
                Dim roles As String = String.Empty
                Select Case PermissionType
                    Case "CanView"
                        roles = f.Security.View
                    Case "CanRead"
                        roles = f.Security.Read
                    Case "CanApprove"
                        roles = f.Security.ModApprove
                    Case Else
                        roles = f.Security.View
                End Select
                Dim canView As Boolean = Permissions.HasPerm(roles, UserRoles)
                If (canView Or (canView = False And f.Hidden = False And (PermissionType = "CanView" Or PermissionType = "CanRead"))) And f.Active Then
                    forumIds &= f.ForumID & ";"
                End If
            Next
            Return forumIds
        End Function

        Friend Function GetUser(ByVal PortalId As Integer, ByVal UserId As Integer) As UserInfo
            Dim u As New UserInfo
            u.UserId = -1
            u.UserRoles = DotNetNuke.Common.glbRoleUnauthUser & ";" & DotNetNuke.Common.glbRoleAllUsers & ";|-1|"
            u.Username = String.Empty
            u.DisplayName = String.Empty
            If UserId > 0 Then
                Dim uc As New DotNetNuke.Entities.Users.UserController
                Dim objUser As DotNetNuke.Entities.Users.UserInfo = uc.GetUser(PortalId, UserId)
                If Not objUser Is Nothing Then
                    u.UserId = objUser.UserID
                    u.Username = objUser.Username
                    u.DisplayName = objUser.DisplayName
                    Dim forumsUC As New UserController
                    Dim tmpUser As User = forumsUC.GetUser(PortalId, -1, objUser.UserID)
                    u.UserRoles = tmpUser.UserRoles

                End If
            End If
            Return u
        End Function
       
    End Class
End Namespace

